package com.pi.base.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pi.base.common.BasePage;
import com.pi.base.common.BaseResult;
import com.pi.base.common.SearchPage;
import com.pi.base.constant.SysStatics;
import com.pi.base.entity.SysMenu;
import com.pi.base.entity.SysRole;
import com.pi.base.entity.SysRoleExample;
import com.pi.base.service.ISysParameterService;
import com.pi.base.service.ISysRoleService;
import com.pi.base.service.ISysUserService;
import com.pi.base.util.JsonTool;
import com.pi.base.util.SysAuthUtil;
import com.pi.base.util.ValidUtil;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController{
	
	@Resource
	private ISysParameterService sysParameterService;
	@Resource
	private ISysRoleService sysRoleService ;
	@Resource
	private ISysUserService sysUserService;
	
	@RequestMapping(value="/getRoleList",method={RequestMethod.POST})
	@ResponseBody
	public String getRoleList(@ModelAttribute SearchPage searchPage,@RequestParam(required=false)String qrName,
			@RequestParam(required=false)String qrState) throws Exception{
	    Integer page = (searchPage.getPage() == null) ? 1 : searchPage.getPage();
	    Integer rows = (searchPage.getRows() == null) ? 0 : searchPage.getRows(); //默认查询全部	   
		SysRoleExample roleExample = new SysRoleExample();
		SysRoleExample.Criteria criteria = roleExample.createCriteria();
		if(!ValidUtil.isBlank(qrName)){
			criteria.andNameLike(new StringBuilder("%").append(qrName).append("%").toString());
		}
		if(ValidUtil.isNumber(qrState)){
			criteria.andStateEqualTo(Integer.valueOf(qrState));
		}
		if(!ValidUtil.isBlank(searchPage.getSort())){
			String sort = ValidUtil.camelToUnderline(searchPage.getSort());
			roleExample.setOrderByClause(sort + " " + searchPage.getOrder());
		}
		BasePage<SysRole> rolePage = sysRoleService.getRoleListByExample(roleExample, page, rows);
		return JsonTool.toJsonString(rolePage);
	}
	
	@RequestMapping(value="/udpateRoleState",method={RequestMethod.POST})
	@ResponseBody
	public BaseResult udpateRoleState(@RequestParam String roleIds,@RequestParam(required=false) Integer roleState) throws Exception{
		roleState = (roleState == null) ? 0 :roleState;
		String[] roleIdArray = roleIds.split(",");
		List<Integer> roleIdList = new ArrayList<Integer>();
		for (String s : roleIdArray) {
			roleIdList.add(Integer.valueOf(s));
		}
		boolean flag = sysRoleService.updateRoleStateByRoleIds(roleIdList,roleState);
		if(flag){
			return BaseResult.newSuccessResult("更新成功");
		}
		return BaseResult.newErrorResult("更新失败");
	}
	
	@RequestMapping(value="/doRoleAdd",method={RequestMethod.POST})
	@ResponseBody
	public BaseResult doRoleAdd(HttpSession httpSession,@ModelAttribute SysRole newRole,@RequestParam String newRoleMenuIds)  throws Exception{
		if(ValidUtil.isBlank(newRoleMenuIds)){
			return BaseResult.newErrorResult("参数错误");
		}
		newRole.setState(SysStatics.ROLE_STATE_NORMAL); //状态：正常
		newRole.setType(0); //角色类型：暂时不使用
		newRole.setCreateby(this.getLoginedUserInfo(httpSession).getId());
		newRole.setCreatetime(new Date());
		sysRoleService.insertRole(newRole, newRoleMenuIds);
		return BaseResult.newSuccessResult("新增成功");
	}
	
	@RequestMapping(value="/getMenuTreeListByRoleId",method={RequestMethod.POST})
	@ResponseBody
	public String getMenuTreeListByRoleId(@RequestParam Integer roleId) throws Exception {
		List<SysMenu> menuList = sysUserService.getAllMenuByRoleIdAndLevel(roleId, 1, 2, 3, 4, 5);
		List<SysMenu> newList_ = SysAuthUtil.turnListToTreeList(menuList, SysMenu.class, "getParentid","0", "getId", "getParentid", "setSubList");
	    String jsonStr = JsonTool.toJsonString(newList_);
	    jsonStr = jsonStr.replaceAll("subList", "children");
	    return jsonStr;
	}
	
	@RequestMapping(value="/doRoleUpdate",method={RequestMethod.POST})
	@ResponseBody
	public BaseResult doRoleUpdate(HttpSession httpSession,@ModelAttribute SysRole updateRole,@RequestParam String updateRoleMenuIds)  throws Exception{
		if(updateRole == null || updateRole.getId() == null || ValidUtil.isBlank(updateRoleMenuIds)){
			return BaseResult.newErrorResult("参数错误");
		}
		sysRoleService.updateRole(updateRole, updateRoleMenuIds);
		return BaseResult.newSuccessResult("更新成功");
	}
	
	@RequestMapping(value="/doRoleRemove",method={RequestMethod.POST})
	@ResponseBody
	public BaseResult doRoleRemove(@RequestParam String roleIds) throws Exception{
		if(roleIds == null){
			return BaseResult.newErrorResult("参数错误");
		}
		String[] roleIdArray = roleIds.split(",");
		List<Integer> roleIdList = new ArrayList<Integer>();
		for (String s : roleIdArray) {
			roleIdList.add(Integer.valueOf(s));
		}
		sysRoleService.deleteRole(roleIdList);
		return BaseResult.newSuccessResult("删除成功");
	}
}
